Skip to content

Conversation

@nitrofski
Copy link
Contributor

Which problem is this PR solving?

Fixes #1506.

Short description of the changes

Enable support for installing field instrumentation on union types.

The "type unwrap" logic was generalized to support unwrapping to a list of types. It is now a recursive function rather than a loop. The new unwrapping has been implemented in a way that is more agreeable to TypeScript.

@nitrofski nitrofski requested a review from a team as a code owner July 3, 2025 16:35
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jul 3, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@github-actions github-actions bot requested a review from obecny July 3, 2025 16:35
@nitrofski nitrofski changed the title fix union types Enable union type support in @opentelemetry/instrumentation-graphql (#1506) Jul 3, 2025
@nitrofski nitrofski changed the title Enable union type support in @opentelemetry/instrumentation-graphql (#1506) fix: Enable union type support in @opentelemetry/instrumentation-graphql (#1506) Jul 3, 2025
@david-luna
Copy link
Contributor

Hi @nitrofski and thanks for your contribution.

I order to proceed we need a couple of things:

  • a recent PR which changes the folder structure has ben merged. Please sync your PR with the latest changes
  • you need to sing the CLA so we can run the CI

Cheers

@dyladan
Copy link
Member

dyladan commented Jul 9, 2025

@obecny can you please take a look at this?

@nitrofski
Copy link
Contributor Author

nitrofski commented Jul 9, 2025

@david-luna

I merged with main.

There are issues with CLA approval at my company at the moment, which we are working on getting resolved. I will sign as soon as that's done. Sorry for the delay.

@nitrofski
Copy link
Contributor Author

nitrofski commented Jul 10, 2025

CLA issues have been resolved.

@dyladan
Copy link
Member

dyladan commented Jul 16, 2025

@obecny can you please take a look at this?

@nitrofski
Copy link
Contributor Author

Good day,

This hasn't seen feedback in over a month. Is there anything I can do to help get attention on this PR?

@codecov
Copy link

codecov bot commented Sep 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.66%. Comparing base (68989f3) to head (115cec5).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2923      +/-   ##
==========================================
- Coverage   92.66%   92.66%   -0.01%     
==========================================
  Files         237      237              
  Lines       11232    11230       -2     
  Branches     2329     2327       -2     
==========================================
- Hits        10408    10406       -2     
  Misses        824      824              
Flag Coverage Δ
auto-instrumentations-node 98.24% <ø> (ø)
instrumentation-graphql 94.10% <100.00%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/instrumentation-graphql/src/utils.ts 93.45% <100.00%> (+0.25%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

): void {
if (
!type ||
typeof type.getFields !== 'function' ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar enough with this library. Are we certain that all possible values of type have a function named getFields?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is part of graphqlTypes.GraphQLObjectType. As long as we maintain type safety and that the type definitions are accurate (to my testing, they were), we should be safe.

The previous implementation used an any type before recursing, so they lost type safety and ended up passing non-compliant object. It had to re-assess the object here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay then


// unwrap union types
if (isGraphQLUnionType(type)) {
return type.getTypes();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I have a similar question. Are we certain that getTypes() returns an array of GraphQLObjectType. Could be possible to have nested unions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair concern. Doing a quick search through the graphql type definitions, GraphQLUnionType is the only type with a getTypes() function, and it always returns an array of GraphQLObjectType. That all being said, is it enough for us or do we want to be extra safe and also validate what the function returns at runtime?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be possible to have nested unions?

Doing a bit of research...

From the Apollo Graphql Union and Interfaces schema help page

All of a union's included types must be object types (not scalars, input types, etc.). Included types do not need to share any fields.

It sounds like unions strictly require their components to be objects and that nested union are not allowed. Though, to dispel any doubts, I tried launching my graphql server with a dummy union type that uses other union types:

Union type MyTestUnion can only include Object types, it cannot include <other union type>.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your thorough explanation @nitrofski :)

@david-luna
Copy link
Contributor

david-luna commented Sep 15, 2025

@nitrofski thank for resolving my doubts. You have my approval but I'll wait a bit in case someone else wants to do a review. @obecny maybe?

@david-luna
Copy link
Contributor

@nitrofski I'm merging this today so it will be included in the upcoming release

Could I ping you if there is any issue in this new version? Thanks :)

@david-luna david-luna merged commit 692cf0a into open-telemetry:main Sep 22, 2025
19 checks passed
@otelbot
Copy link
Contributor

otelbot bot commented Sep 22, 2025

Thank you for your contribution @nitrofski! 🎉 We would like to hear from you about your experience contributing to OpenTelemetry by taking a few minutes to fill out this survey.

@nitrofski
Copy link
Contributor Author

Could I ping you if there is any issue in this new version? Thanks :)

Sure, I can help. FYI I have another PR on the same package coming.

@nitrofski nitrofski deleted the dev/fortinv/fix-union-types branch September 22, 2025 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GraphQL field instrumentation does not work for union return types

4 participants